package com.chen.positioning.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 直接引用 jdk版本1.8方式進行加解密
 * @author halo
 */
@Service
public class Base64Util {
    private static final Logger logger = LoggerFactory.getLogger(Base64Util.class);

    /**
     * 加密
     *
     * @param str
     * @return
     * @throws Exception
     */
    public String encryptionByBase64(String str) throws Exception {
        BASE64Encoder encoder = new BASE64Encoder();
        String encode = encoder.encode(str.getBytes());
        System.out.println("加密后的内容：" + encode);
        logger.info("加密后的内容：" + encode);
        return encode;
    }

    /**
     * 解密
     *
     * @param str
     * @return
     * @throws Exception
     */
    public String decryptByBase64(String str) throws Exception {
        BASE64Decoder decoder = new BASE64Decoder();
        String decryptStr = new String(decoder.decodeBuffer(str));
        logger.info("解密后的内容 ====" + decryptStr);
        return decryptStr;
    }

    public static void main(String[] args) {
        Base64Util base64Util = new Base64Util();
        try {
            String str="1";
            logger.info(String.valueOf(str.length()));
            String enStr=base64Util.encryptionByBase64(str);
            logger.info(String.valueOf(enStr.length()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
